import {Message, MessageBox, Loading, Alert} from "element-ui";
import store from "@/store";
import {getStore} from "./store";
import {socketURL, imagePath} from "@/config/env.js";

let webSocket = null;
let mAudio = null;
let webSocketStateInterval = null;
let heartInterval = null;
let _closeType = false; // true主动挂断 false被动挂断
let tiAlarm = 0;
let missedAlarm = 0;

export default {
  initWebsocket() {
    let _that = this;
    let userInfo = getStore({name: "userInfo"});
    let _url = socketURL + userInfo.userId;
    webSocket = new WebSocket(_url);
    webSocket.onopen = function (event) {
      // console.log("webSocket onopen ----->", event);

      heartInterval = setInterval(function () {
        webSocket.send(JSON.stringify({cmd: 100}));
      }, 10000);

      webSocketStateInterval = setInterval(function () {
        switch (webSocket.readyState) {
          case WebSocket.CONNECTING:
            console.log("webSocket 正在连接---->");
            break;
          case WebSocket.OPEN:
            console.log("webSocket 打开状态---->");
            break;
          case WebSocket.CLOSING:
            console.log("webSocket 正在关闭---->");
            break;
          case WebSocket.CLOSED:
            console.log("webSocket 关闭成功---->");
            if (webSocketStateInterval) {
              clearInterval(webSocketStateInterval);
            }
            if (_closeType == false) {
              _that.initWebsocket();
            }
            break;
          default:
            console.log("webSocket-------");
            break;
        }
      }, 2000);
    };
    webSocket.onmessage = function (event) {
      // console.log("webSocket onmessage ----->", event);
      let message = JSON.parse(event.data);
      console.log("message----->", message);
      if (message.cmd == 1) {
        mAudio = new Audio();
        mAudio.src = imagePath + "/newcall.mp3";
        mAudio.play();
        mAudio.loop = true;
        store.commit("setComingState", true);
        store.commit("setIncomingMessage", message.msg)
      } else if (message.cmd == 7) {
        // MessageBox.confirm("对方已挂断", "提示", {confirmButtonText: "确定"});
        Message({
          message: "对方已挂断",
          type: "error",
        });
        mAudio.pause();
        store.dispatch("hangUpActions");
      } else if (message.cmd == 9) { // 签字
        store.commit('setPersonInfoList', message.personInfoList);
        store.commit("setShowSign", true);
      } else if (message.cmd == 40) { // 有新的图文报警消息
        tiAlarm++
        store.commit('setTiAlarm', tiAlarm);
        mAudio = new Audio();
        mAudio.src = imagePath + "/ti-alarm.mp3";
        mAudio.play();
      } else if (message.cmd == 41) { // 有新的视频报警漏接消息
        missedAlarm++
        store.commit('setMissedAlarm', missedAlarm);
        mAudio = new Audio();
        mAudio.src = imagePath + "/missed-alarm.mp3";
        mAudio.play();
      } else if (message.cmd == 100) {
        console.log("100-心跳中------", message.msg);
      }
    };
    webSocket.onerror = function (event) {
      console.log("webSocket onerror ----->", event);
      if (_closeType) {
        if (webSocketStateInterval) {
          clearInterval(webSocketStateInterval);
        }
      } else {
        _that.initWebsocket();
      }
    };
    webSocket.onclose = function (event) {
      console.log("webSocket onclose --->", event);
      if (_closeType) {
        if (webSocketStateInterval) {
          clearInterval(webSocketStateInterval);
        }
      }
      if (heartInterval) {
        clearInterval(heartInterval);
      }
      _closeType = false;
    };
    // return new Promise((resolve, reject) => {resolve()});
  },
  sendWebSocket(text) {
    webSocket.send(JSON.stringify(text));
  },
  closeWebSocket(closeType) {
    _closeType = closeType;
    if (webSocket) {
      webSocket.close();
    }
  },
  closeAudio() {
    mAudio.pause();
  },
  initTiAlarm() {
    tiAlarm = 0;
  },
  initMissedAlarm() {
    missedAlarm = 0;
  }
}
